package com.contractpage.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.common.util.ConfigUtil;
import com.contractpage.util.SpringContextHolder;
import com.contractpage.util.StringUtils;
import com.contractpage.util.UserUtils;
import com.rbac.service.PrivilegeService;
import com.richgo.util.Tool;
import com.user.entity.emp.EmpVO;
import com.user.service.emp.EmpService;

/**
 * 当成功登录SSO系统时将会返回登录的userid根据此userid建立session会话;
 * 
 *
 */
@WebFilter(urlPatterns = "/*", filterName = "SSOFilter")
public class SSO4InvokeContextFilter implements Filter {
	private WebApplicationContext applicationContext;
	@Autowired
	private static EmpService empService = SpringContextHolder.getBean(EmpService.class);
	@Autowired
	private static PrivilegeService privilegeService = SpringContextHolder.getBean(PrivilegeService.class);

	public SSO4InvokeContextFilter() {
		super();
	}

	/**
	 * 过滤器注销时，触发此方法;
	 */
	@Override
	public void destroy() {
		// 暂时不做任何处理;
	}
	/**
	 * 根据用户id获取用户信息并且把用户信息放入session会话中; @Title: doFilter @Description:
	 * TODO(这里用一句话描述这个方法的作用) @Params @throws
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		if(true){
			EmpVO empVO = UserUtils.getEmpVO() == null?empService.selectLoginUserInfo("H018441"):UserUtils.getEmpVO();
			if(empVO != null){
				request.getSession().setAttribute("user", empVO);
			}
			chain.doFilter(request, response);
			return;
		}
		Object obj = request.getSession().getAttribute("user");
		if(obj != null){
			chain.doFilter(request, response);
			return;
		}
		Cookie cookie = Tool.ReadCookieMap(request).get("token");
		if (cookie == null) {
			response.sendRedirect(ConfigUtil.getValue("sso.web.site"));
			return;
		}
		String code = empService.verificationTokenRetCode(cookie.getValue());
		if (StringUtils.isEmpty(code)) {
			response.sendRedirect(ConfigUtil.getValue("sso.web.site"));
			return;
		}
		EmpVO empVO = empService.selectLoginUserInfo(code);
		if(empVO == null){
			response.sendRedirect(ConfigUtil.getValue("sso.web.site"));
			return;
		}
		request.getSession().setAttribute("user", empVO);
		/*EmpVO empVO = empService.selectLoginUserInfo(code);
		subject.login(token);*/
		chain.doFilter(request, response);
	}

	/**
	 * 初始化Spring上下文;
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		WebApplicationContext applicationContext = WebApplicationContextUtils
				.getWebApplicationContext(filterConfig.getServletContext());
		this.applicationContext = applicationContext;
	}
}